package com.vito.dcs.acquisition.blender;

import java.text.DecimalFormat;
import java.util.Date;

import com.vito.base.util.DateUtils;
import com.vito.dcs.asphalt.asphaltData.model.DataAsphaltBlenderModel;
import com.vito.dcs.water.waterData.model.DataWaterBlenderModel;

/***
 * 拌合站数据采集工具类
 * @author hug19
 *
 */
public class UdpUtil {
	
	
	/**
	 * 校验沥青或水稳搅拌上报数据的合法性
	 * @param hexString
	 * @return
	 */
	public static boolean checkBlenderData(String hexString) {
		String headStr = hexString.substring(0,2);
		String endStr = hexString.substring(hexString.length()-4,hexString.length());
		String dataStr = hexString.substring(2,hexString.length()-4);
		String dataLenStr = dataStr.substring(0,4);
		int dataLenInt = Integer.valueOf(dataLenStr, 16);
		if("EB".equals(headStr)&&"A1B2".equals(endStr)&&(dataLenInt*2==dataStr.length()-4)) {
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 解析水稳搅拌上报数据
	 * @param hexString
	 * @return
	 */
	public static DataWaterBlenderModel analysisWater(String hexString) {
		
		String headStr = hexString.substring(0,2);
		String endStr = hexString.substring(hexString.length()-4,hexString.length());
		String dataStr = hexString.substring(2,hexString.length()-4);
		String dataLenStr = dataStr.substring(0,4);
		int dataLenInt = Integer.valueOf(dataLenStr, 16);

		DataWaterBlenderModel waterData = null;
		String temp = null;
		if("EB".equals(headStr)&&"A1B2".equals(endStr)&&(dataLenInt*2==dataStr.length()-4)) {
			
			waterData = new DataWaterBlenderModel();
			temp = dataStr.substring(4,8);
			String versionNo = Integer.valueOf(temp, 16)+"";
//			System.out.println(versionNo);
			waterData.setVersionNo(versionNo);
			dataStr = dataStr.substring(8);
			//截取项目、标段、拌合站、拌合机编码
			temp = dataStr.substring(0,2);
			String projectNo = Integer.valueOf(temp, 16)+"";
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String sectionNo = Integer.valueOf(temp, 16)+"";
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String stationNo = Integer.valueOf(temp, 16)+"";
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String machineNo = Integer.valueOf(temp, 16)+"";	
//			System.out.println(projectNo+"-"+sectionNo+"-"+stationNo+"-"+machineNo);
			waterData.setProjectNo(projectNo);
			waterData.setSectionNo(sectionNo);
			waterData.setStationNo(stationNo);
			waterData.setMachineNo(machineNo);
			waterData.setCodeNum(projectNo+"-"+sectionNo+"-"+stationNo+"-"+machineNo);
			dataStr = dataStr.substring(2);
			//截取日期
			temp = dataStr.substring(0,2);
			String year = Integer.valueOf(temp, 16)+"";
			year = year.length()==3?"2"+year:"20"+year;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String month = Integer.valueOf(temp, 16)+"";
			month = month.length()==2?month:"0"+month;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String day = Integer.valueOf(temp, 16)+"";
			day = day.length()==2?day:"0"+day;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String hour = Integer.valueOf(temp, 16)+"";
			hour = hour.length()==2?hour:"0"+hour;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String minute = Integer.valueOf(temp, 16)+"";
			minute = minute.length()==2?minute:"0"+minute;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String second = Integer.valueOf(temp, 16)+"";
			second = second.length()==2?second:"0"+second;
//			System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
			dataStr = dataStr.substring(2);
			Date makeTime = DateUtils.formatToDateTime(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
			waterData.setMixDate(makeTime);
			waterData.setUpDate(new Date());
			//跳过配比编号
			temp = dataStr.substring(0,2);
			if(Integer.valueOf(temp, 16)==0) {
				dataStr = dataStr.substring(2);
			}else {
				dataStr = dataStr.substring(2+Integer.valueOf(temp, 16)*2);
			}
			
			//数据项
			while(dataStr.length()>0) {
				
				temp = dataStr.substring(0,34);			
				setWaterOption(temp,waterData);
				dataStr = dataStr.substring(34);

			}
			
		}
		return waterData;
	}
	
	/**
	 * 解析设置水稳搅拌数据项
	 * @param optionStr
	 * @param waterData
	 */
	private static void setWaterOption(String optionStr,DataWaterBlenderModel waterData) {
		
		int optionInt;
		String designStr = null;
		String designRatioStr = null;
		String actualStr = null;
		String actualRatioStr = null;
		DecimalFormat df = new DecimalFormat("#.######");
		optionInt = Integer.valueOf(optionStr.substring(0,2), 16);
		designStr = optionStr.substring(2,10);
		if(Integer.valueOf(designStr.substring(0,6), 16)>0) {
			designStr = Integer.valueOf(designStr.substring(0,6), 16)*1d/Math.pow(10,Integer.valueOf(
					designStr.substring(6), 16))+"";
		}else {
			designStr = "0";
		}
		designRatioStr = optionStr.substring(10,18);
		if(Integer.valueOf(designRatioStr.substring(0,6), 16)>0) {
			designRatioStr = df.format(Integer.valueOf(designRatioStr.substring(0,6), 16)*1d/Math.pow(10,Integer.valueOf(
					designRatioStr.substring(6), 16))/100d);
		}else {
			designRatioStr = "0";
		}
		actualStr = optionStr.substring(18,26);
		if(Integer.valueOf(actualStr.substring(0,6), 16)>0) {
			actualStr = Integer.valueOf(actualStr.substring(0,6), 16)*1d/Math.pow(10,Integer.valueOf(
					actualStr.substring(6), 16))+"";
		}else {
			actualStr = "0";
		}
		actualRatioStr = optionStr.substring(26);
		if(Integer.valueOf(actualRatioStr.substring(0,6), 16)>0) {
			actualRatioStr = df.format(Integer.valueOf(actualRatioStr.substring(0,6), 16)*1d/Math.pow(10,Integer.valueOf(
					actualRatioStr.substring(6), 16))/100d);
		}else {
			actualRatioStr = "0";
		}
//		System.out.println("项目值:"+optionStr+" 设计值:"+designStr+" 设计配比:"+designRatioStr+
//		" 实际值:"+actualStr+" 实际配比:"+actualRatioStr);
		switch (optionInt) {
		case 1:
			waterData.setAggregate1Design(designStr);
			waterData.setAggregate1DesignRatio(designRatioStr);
			waterData.setAggregate1Actual(actualStr);
			waterData.setAggregate1ActualRatio(actualRatioStr);
			break;
		case 2:
			waterData.setAggregate2Design(designStr);
			waterData.setAggregate2DesignRatio(designRatioStr);
			waterData.setAggregate2Actual(actualStr);
			waterData.setAggregate2ActualRatio(actualRatioStr);
			break;
		case 3:
			waterData.setAggregate3Design(designStr);
			waterData.setAggregate3DesignRatio(designRatioStr);
			waterData.setAggregate3Actual(actualStr);
			waterData.setAggregate3ActualRatio(actualRatioStr);
			break;
		case 4:
			waterData.setAggregate4Design(designStr);
			waterData.setAggregate4DesignRatio(designRatioStr);
			waterData.setAggregate4Actual(actualStr);
			waterData.setAggregate4ActualRatio(actualRatioStr);
			break;
		case 5:
			waterData.setAggregate5Design(designStr);
			waterData.setAggregate5DesignRatio(designRatioStr);
			waterData.setAggregate5Actual(actualStr);
			waterData.setAggregate5ActualRatio(actualRatioStr);
			break;
		case 6:
			waterData.setAggregate6Design(designStr);
			waterData.setAggregate6DesignRatio(designRatioStr);
			waterData.setAggregate6Actual(actualStr);
			waterData.setAggregate6ActualRatio(actualRatioStr);
			break;
		case 7:
			waterData.setCement1Design(designStr);
			waterData.setCement1DesignRatio(designRatioStr);
			waterData.setCement1Actual(actualStr);
			waterData.setCement1ActualRatio(actualRatioStr);
			break;
		case 8:
			waterData.setCement2Design(designStr);
			waterData.setCement2DesignRatio(designRatioStr);
			waterData.setCement2Actual(actualStr);
			waterData.setCement2ActualRatio(actualRatioStr);
			break;
		case 9:
			waterData.setCement3Design(designStr);
			waterData.setCement3DesignRatio(designRatioStr);
			waterData.setCement3Actual(actualStr);
			waterData.setCement3ActualRatio(actualRatioStr);
			break;
		case 10:
			waterData.setWaterDesign(designStr);
			waterData.setWaterDesignRatio(designRatioStr);
			waterData.setWaterActual(actualStr);
			waterData.setWaterActualRatio(actualRatioStr);
			break;
		default:
			break;
		}

		
	}
	
	/**
	 * 解析沥青搅拌上报数据
	 * @param hexString
	 * @return
	 */
	public static DataAsphaltBlenderModel  analysisAsphalt(String hexString) {
		
		String headStr = hexString.substring(0,2);
		String endStr = hexString.substring(hexString.length()-4,hexString.length());
		String dataStr = hexString.substring(2,hexString.length()-4);
		String dataLenStr = dataStr.substring(0,4);
		int dataLenInt = Integer.valueOf(dataLenStr, 16);

		DataAsphaltBlenderModel asphaltData = null;
		String temp = null;
		if("EB".equals(headStr)&&"A1B2".equals(endStr)&&(dataLenInt*2==dataStr.length()-4)) {
			
			asphaltData = new DataAsphaltBlenderModel();
			temp = dataStr.substring(4,8);
			asphaltData.setVersionNo(Integer.valueOf(temp, 16)+"");
			dataStr = dataStr.substring(8);
			//截取项目、标段、拌合站、拌合机编码
			temp = dataStr.substring(0,2);
			String projectNo = Integer.valueOf(temp, 16)+"";
			asphaltData.setProjectNo(projectNo);
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String sectionNo = Integer.valueOf(temp, 16)+"";
			asphaltData.setSectionNo(sectionNo);
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String stationNo = Integer.valueOf(temp, 16)+"";
			asphaltData.setStationNo(stationNo);
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String machineNo = Integer.valueOf(temp, 16)+"";
			asphaltData.setMachineNo(machineNo);
			asphaltData.setCodeNum(projectNo+"-"+sectionNo+"-"+stationNo+"-"+machineNo);
//			System.out.println(projectNo+"-"+sectionNo+"-"+stationNo+"-"+machineNo);
			dataStr = dataStr.substring(2);
			//截取日期
			temp = dataStr.substring(0,2);
			String year = Integer.valueOf(temp, 16)+"";
			year = year.length()==3?"2"+year:"20"+year;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String month = Integer.valueOf(temp, 16)+"";
			month = month.length()==2?month:"0"+month;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String day = Integer.valueOf(temp, 16)+"";
			day = day.length()==2?day:"0"+day;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String hour = Integer.valueOf(temp, 16)+"";
			hour = hour.length()==2?hour:"0"+hour;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String minute = Integer.valueOf(temp, 16)+"";
			minute = minute.length()==2?minute:"0"+minute;
			dataStr = dataStr.substring(2);
			temp = dataStr.substring(0,2);
			String second = Integer.valueOf(temp, 16)+"";
			second = second.length()==2?second:"0"+second;
//			System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
			Date makeTime = DateUtils.formatToDateTime(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
			asphaltData.setMixDate(makeTime);
			asphaltData.setUpDate(new Date());
			dataStr = dataStr.substring(2);
			//设计油石比
			temp = dataStr.substring(0,4);
			String oilstoneRatio = Integer.valueOf(temp.substring(0,2), 16)+"";
			if(!oilstoneRatio.equals("0")) {
				oilstoneRatio = (Double.valueOf(oilstoneRatio)/Math.pow(10,Integer.valueOf(temp.substring(2), 16)))+"";
			}
//			System.out.println("设计油石比:"+oilstoneRatio);
			asphaltData.setOilstoneRatio(oilstoneRatio);
			dataStr = dataStr.substring(4);
			//温度1
			temp = dataStr.substring(0,8);
			String temperature1 = Integer.valueOf(temp.substring(0,6), 16)+"";
			if(!temperature1.equals("0")) {
				temperature1 = (Double.valueOf(temperature1)/Math.pow(10,Integer.valueOf(temp.substring(6), 16)))+"";
			}
//			System.out.println("温度1:"+temperature1);
			asphaltData.setTemperature1(temperature1);
			dataStr = dataStr.substring(8);	
			//温度2
			temp = dataStr.substring(0,8);
			String temperature2 = Integer.valueOf(temp.substring(0,6), 16)+"";
			if(!temperature2.equals("0")) {
				temperature2 = (Double.valueOf(temperature2)/Math.pow(10,Integer.valueOf(temp.substring(6), 16)))+"";
			}
//			System.out.println("温度2:"+temperature2);
			asphaltData.setTemperature2(temperature2);
			dataStr = dataStr.substring(8);
			//温度3
			temp = dataStr.substring(0,8);
			String temperature3 = Integer.valueOf(temp.substring(0,6), 16)+"";
			if(!temperature3.equals("0")) {
				temperature3 = (Double.valueOf(temperature3)/Math.pow(10,Integer.valueOf(temp.substring(6), 16)))+"";
			}
//			System.out.println("温度3:"+temperature3);
			asphaltData.setTemperature3(temperature3);
			dataStr = dataStr.substring(8);
			//跳过备用
			dataStr = dataStr.substring(64);
			//数据项
			while(dataStr.length()>0) {
				temp = dataStr.substring(0,18);
				setAsphaltOption(temp,asphaltData);
				dataStr = dataStr.substring(18);
				
			}
			asphaltData.setOilstoneRatioActual(asphaltData.getOilTotal()/asphaltData.getStoneTotal()+"");
		}
		return asphaltData;
		
	}
	
	/**
	 * 解析设置沥青搅拌数据项
	 * @param optionStr
	 * @param asphaltData
	 */
	private static void setAsphaltOption(String optionStr,DataAsphaltBlenderModel asphaltData) {
		
		int optionInt;
		String designStr = null;
		String actualStr = null;
		optionInt = Integer.valueOf(optionStr.substring(0,2), 16);
		designStr = optionStr.substring(2,10);
		if(Integer.valueOf(designStr.substring(0,6), 16)>0) {
			designStr = Integer.valueOf(designStr.substring(0,6), 16)*1d/Math.pow(10,Integer.valueOf(
					designStr.substring(6), 16))+"";
		}else {
			designStr = "0";
		}
		actualStr = optionStr.substring(10);
		if(Integer.valueOf(actualStr.substring(0,6), 16)>0) {
			actualStr = Integer.valueOf(actualStr.substring(0,6), 16)*1d/Math.pow(10,Integer.valueOf(
					actualStr.substring(6), 16))+"";
		}else {
			actualStr = "0";
		}
//		System.out.println("项目值:"+optionInt+" 设计值:"+designStr+" 实际值:"+actualStr);
		Double stoneTotal = asphaltData.getStoneTotal()==null?0d:asphaltData.getStoneTotal();
		Double oilTotal = asphaltData.getOilTotal()==null?0d:asphaltData.getOilTotal();
		switch (optionInt) {
		case 1:
			asphaltData.setAggregate1Actual(actualStr);
			asphaltData.setAggregate1Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 2:
			asphaltData.setAggregate2Actual(actualStr);
			asphaltData.setAggregate2Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 3:
			asphaltData.setAggregate3Actual(actualStr);
			asphaltData.setAggregate3Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 4:
			asphaltData.setAggregate4Actual(actualStr);
			asphaltData.setAggregate4Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 5:
			asphaltData.setAggregate5Actual(actualStr);
			asphaltData.setAggregate5Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 6:
			asphaltData.setAggregate6Actual(actualStr);
			asphaltData.setAggregate6Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 7:
			asphaltData.setPowder1Actual(actualStr);
			asphaltData.setPowder1Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 8:
			asphaltData.setPowder2Actual(actualStr);
			asphaltData.setPowder2Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 9:
			asphaltData.setPowder3Actual(actualStr);
			asphaltData.setPowder3Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 10:
			asphaltData.setPowder4Actual(actualStr);
			asphaltData.setPowder4Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 11:
			asphaltData.setAsphalt1Actual(actualStr);
			asphaltData.setAsphalt1Design(designStr);
			oilTotal = oilTotal +Double.valueOf(actualStr);
			break;
		case 12:
			asphaltData.setAsphalt2Actual(actualStr);
			asphaltData.setAsphalt2Design(designStr);
			oilTotal = oilTotal +Double.valueOf(actualStr);
			break;
		case 13:
			asphaltData.setAsphalt3Actual(actualStr);
			asphaltData.setAsphalt3Design(designStr);
			oilTotal = oilTotal +Double.valueOf(actualStr);
			break;
		case 14:
			asphaltData.setAsphalt4Actual(actualStr);
			asphaltData.setAsphalt4Design(designStr);
			oilTotal = oilTotal +Double.valueOf(actualStr);
			break;
		case 15:
			asphaltData.setAsphalt5Actual(actualStr);
			asphaltData.setAsphalt5Design(designStr);
			oilTotal = oilTotal +Double.valueOf(actualStr);
			break;
		case 16:
			asphaltData.setAdditive1Actual(actualStr);
			asphaltData.setAdditive1Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		case 17:
			asphaltData.setAdditive2Actual(actualStr);
			asphaltData.setAdditive2Design(designStr);
			stoneTotal = stoneTotal +Double.valueOf(actualStr);
			break;
		default:
			break;
		}
		asphaltData.setStoneTotal(stoneTotal);
		asphaltData.setOilTotal(oilTotal);
		
	}

	/**
	 * 字节数组转十六进展字符串
	 * @param bytes
	 * @return
	 */
	public static String bytesToHexString(byte[] bytes){   
	    StringBuilder stringBuilder = new StringBuilder("");   
	    if (bytes == null || bytes.length <= 0) {   
	        return null;   
	    }   
	    for (int i = 0; i < bytes.length; i++) {   
	        int v = bytes[i] & 0xFF;   
	        String hv = Integer.toHexString(v);   
	        if (hv.length() < 2) {   
	            stringBuilder.append(0);   
	        }   
	        stringBuilder.append(hv);   
	    }   
	    return stringBuilder.toString().toUpperCase();   
	}
	
	/**
	 * 十六进制字符串转字节数组
	 * @param hexString
	 * @return
	 */
	public static byte[] hexStringToBytes(String hexString) {   
	    if (hexString == null || hexString.equals("")) {   
	        return null;   
	    }   
	    hexString = hexString.toUpperCase();   
	    int length = hexString.length() / 2;   
	    char[] hexChars = hexString.toCharArray();   
	    byte[] d = new byte[length];   
	    for (int i = 0; i < length; i++) {   
	        int pos = i * 2;   
	        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));   
	    }   
	    return d;   
	}
	
	private static byte charToByte(char c) {   
	    return (byte) "0123456789ABCDEF".indexOf(c);   
	} 

}
